package com.heatup.apt.api.service.impl;

import com.heatup.apt.api.service.PurchaseCourseVipRightsLogService;
import com.heatup.apt.common.dao.user.UserCourseVipServiceMapper;
import com.heatup.apt.common.util.DateUtils;
import com.heatup.apt.model.dto.UserVipServiceDTO;
import com.heatup.apt.model.order.PurchaseCourseVipRightsLog;
import com.heatup.apt.model.user.UserCourseVipService;
import com.heatup.apt.model.user.UserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.heatup.apt.api.service.UserCourseVipServiceService;
import com.baomidou.framework.service.impl.SuperServiceImpl;

import java.util.Calendar;
import java.util.Date;

/**
 *
 * UserCourseVipService 表数据服务层接口实现类
 *
 */
@Service
public class UserCourseVipServiceServiceImpl extends SuperServiceImpl<UserCourseVipServiceMapper, UserCourseVipService> implements UserCourseVipServiceService {

    @Autowired
    private PurchaseCourseVipRightsLogService purchaseCourseVipRightsLogService;

    // 有效状态
    final static int EFFECTIVE_STATE = 1;
    // 无效状态
    final static int INVALID_STATE = 0;

    /**
     * @Author: timo
     * @Date: 2018/7/14 16:46
     * @Description: 查询用户VIP服务权益
     * @param userId 用户ID
     * @param serviceType 服务类型 1 团课 2 私教 3 檬营 4 自助健身
     * @param status 1 有效 0 无效 null 查询存在的
     * @return:
     *
     */
    @Override
    public UserCourseVipService queryUserCourseVipService(Integer userId, Integer serviceType, Integer status) {
        UserCourseVipService uc = new UserCourseVipService();
        uc.setUserId(userId);
        uc.setServiceType(serviceType);
        if (status != null){
            uc.setStatus(status);
        }
        uc = baseMapper.selectOne(uc);
        return uc;
    }

    /**
     * @Author: timo
     * @Date: 2018/7/14 16:53
     * @Description: 保存用户拥有VIP的服务类型权益
     * @param userInfo 用户
     * @param saveDTO  vip 服务
     * @return:
     *
     */
    @Override
    public boolean saveUserCourseVipService(UserInfo userInfo, UserVipServiceDTO saveDTO) {
        // step 1 先查询该用户是否拥有改服务权益
        Date nowDateTime = new Date();
        Date nowDate = DateUtils.getDateShort(nowDateTime);
        Integer status = EFFECTIVE_STATE;
        UserCourseVipService userCourseVip = queryUserCourseVipService(saveDTO.getUserId(), saveDTO.getServiceType(), null);

        // 开卡日期做一下处理
        // 如果是延迟开卡就是开卡日期不是延迟开卡就是今天
        Date openCarDate = saveDTO.getOpenDate() == null ? nowDate : DateUtils.getDateShort(saveDTO.getOpenDate());
        // 判断是否存在
        if (userCourseVip != null){
            // 正在使用中
            if (userCourseVip.getStatus().intValue() == EFFECTIVE_STATE){
                // 正在使用期间
                // 如果是使用期间当用户直接在结束时间后面添加 不需要管开卡时间
                // 将结束时间当作开始时间
                // 直接覆盖
                saveDTO.setStartDate(userCourseVip.getEndDate());
                userCourseVip.setEndDate(saveDTO.getEndDate());

            }else {
                // 未在使用中

                // 已经开通了但是未到生效时间
                // 延迟开卡的续费
                if (userCourseVip.getStartDate() != null && userCourseVip.getStartDate().compareTo(nowDate) > 0){
                    // 将结束时间当作开始时间
                    saveDTO.setStartDate(userCourseVip.getEndDate());
                    userCourseVip.setEndDate(saveDTO.getEndDate());
                }else if (userCourseVip.getStartDate() != null && userCourseVip.getStartDate().compareTo(nowDate) == 0){
                    // 今天正好是生效时间
                    userCourseVip.setStatus(EFFECTIVE_STATE);
                    // 将结束时间当作开始时间
                    saveDTO.setStartDate(userCourseVip.getEndDate());
                    userCourseVip.setEndDate(saveDTO.getEndDate());

                }else {
                    // 已过期
                    // 如果存在延迟开卡的就暂时不给予开卡状态显示为无效的
                    if (nowDate.compareTo(openCarDate) < 0){
                        nowDate = openCarDate;
                        status = INVALID_STATE;
                    }
                    userCourseVip.setStatus(status);
                    // 开卡时间作为开始时间
                    saveDTO.setStartDate(nowDate);
//                    saveDTO.setEndDate(addMonth(saveDTO.getStartDate(),saveDTO.getMonthCount()));
                    userCourseVip.setEndDate(saveDTO.getEndDate());
                }
            }

            userCourseVip.setUpdateBy(userInfo.getId());
            userCourseVip.setUpdateTime(new Date());
            if (this.updateById(userCourseVip)){
                // 插入日志表
                PurchaseCourseVipRightsLog vipLog = new PurchaseCourseVipRightsLog();
                BeanUtils.copyProperties(saveDTO,vipLog);
                vipLog.setCreateBy(userInfo.getId());
                vipLog.setCreateTime(nowDateTime);

                // 插入日志表
                if (purchaseCourseVipRightsLogService.insert(vipLog)){
                    return true;
                }
            }

        }else {
            // 不存在
            // 用户可能当天开卡
            // 还有一种情况就是延迟开卡的

            if (saveDTO.getOpenDate() != null && nowDate.compareTo(openCarDate) < 0){
                nowDate = openCarDate;
                status = 0;
            }
            // 用户未到开卡时间
            if (saveDTO.getStartDate() != null && saveDTO.getStartDate().compareTo(nowDate) > 0){
                nowDate = saveDTO.getStartDate();
                status = 0;
            }
            userCourseVip = new UserCourseVipService();
            userCourseVip.setStatus(status);
            userCourseVip.setUserId(saveDTO.getUserId());
            userCourseVip.setServiceType(saveDTO.getServiceType());
            userCourseVip.setStartDate(nowDate);
            userCourseVip.setEndDate(saveDTO.getEndDate());
            userCourseVip.setCreateBy(saveDTO.getUserId());
            userCourseVip.setCreateTime(nowDateTime);
            if (this.insert(userCourseVip)){

                // 插入日志表
                PurchaseCourseVipRightsLog vipLog = new PurchaseCourseVipRightsLog();
                BeanUtils.copyProperties(saveDTO,vipLog);
                BeanUtils.copyProperties(userCourseVip,vipLog);
                if (purchaseCourseVipRightsLogService.insert(vipLog)){
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * @Author: timo
     * @Date: 2018/7/14 23:04
     * @Description: 判断该用户是否拥VIP服务
     * 根据服务类型来划分
     * @param
     * @return:
     *
     */
    @Override
    public boolean hasUserCourseVipService(Integer userId, Integer serviceType) {
        UserCourseVipService userCourseVip = queryUserCourseVipService(userId,serviceType,EFFECTIVE_STATE);
        if (userCourseVip != null && userCourseVip.getStatus().intValue() == EFFECTIVE_STATE){
            return true;
        }
        return false;
    }

    public Date addMonth(Date startDate,Integer monthCount) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(startDate);
        rightNow.add(Calendar.MONTH, monthCount);// 日期加1个月
        return rightNow.getTime();
    }
}